package com.heimali.happyreader.ui;

import java.io.UnsupportedEncodingException;

import com.heimali.happyreader.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class QPasswordEditText extends LinearLayout {

	private final static String TAG = "QPasswordEditText";
	private EditText input;
	private ImageButton delBtn;

	public QPasswordEditText(Context context) {
		this(context, null);
	}

	public QPasswordEditText(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public QPasswordEditText(final Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		RelativeLayout view = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.qpet_view, null);
		delBtn = (ImageButton) view.findViewById(R.id.qpet_delete);
		delBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				input.setText("");
				delBtn.setVisibility(View.INVISIBLE);
			}
		});

		int resourceID = -1;
		input = (EditText) view.findViewById(R.id.qpet_input);
		input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20), new YunpanPasswordInputFilter() });
		TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.QPasswordEditText);
		resourceID = arr.getResourceId(R.styleable.QPasswordEditText_hint, 0);
		input.setHint(resourceID > 0 ? arr.getResources().getText(resourceID) : arr
				.getString(R.styleable.QPasswordEditText_hint)); // hint不给默认值

		resourceID = arr.getResourceId(R.styleable.QPasswordEditText_textSize, 0);
		CharSequence textSize = resourceID > 0 ? arr.getResources().getText(resourceID) : arr
				.getString(R.styleable.QPasswordEditText_textSize);
		float defaultTextSize = 16.0f;
		input.setTextSize(textSize != null ? Float.valueOf(textSize.toString()) : defaultTextSize);

		input.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				String inputStr = input.getText().toString();
				delBtn.setVisibility(inputStr.length() > 0 ? View.VISIBLE : View.INVISIBLE);
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
		input.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					String inputStr = input.getText().toString();
					delBtn.setVisibility(inputStr.length() > 0 ? View.VISIBLE : View.INVISIBLE);
				} else {
					delBtn.setVisibility(View.INVISIBLE);
				}
			}
		});
		addView(view);
		arr.recycle();
	}

	public Editable getText() {
		return input.getText();
	}

	public void setText(String value) {
		input.setText(value);
	}

	public boolean setFocus() {
		return input.requestFocus();
	}

	public void setOnEditorActionListener(EditText.OnEditorActionListener callback) {
		input.setOnEditorActionListener(callback);

	}

	public class YunpanPasswordInputFilter implements InputFilter {

		public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
			if (isCN(source.toString())) {
				input.setSelection(dstart);
				return "";
			} else {
				return source;
			}
		}

		public boolean isCN(String str) {
			try {
				byte[] bytes = str.getBytes("UTF-8");
				if (bytes.length == str.length()) {
					return false;
				} else {
					return true;
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return false;
		}
	}
}
